

//取得cookie    
function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';'); //把cookie分割成组    
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i]; //取得字符串    
        while (c.charAt(0) == ' ') { //判断一下字符串有没有前导空格    
            c = c.substring(1, c.length); //有的话，从第二位开始取    
        }
        if (c.indexOf(nameEQ) == 0) { //如果含有我们要的name    
            return unescape(c.substring(nameEQ.length, c.length)); //解码并截取我们要值    
        }
    }
    return false;
}




window.onload = function () {
    if (!getCookie('Token')) {
        //清除
        ajaxRequest("php/logout.php", 'get', null, function () {
            console.log("SESSION CLEAR: " + this.responseText);
            localStorage.clear();
            isLogin();
        }, function(){
            console.log(this.responseText);
        });
    }

    function ok() {
        console.log(this.responseText);
    }

    function ko() {
        console.log(this.responseText);
    }

    function isDBExist() {
        ajaxRequest('php/import_db.php', 'get', null, ok, ko);

    }

    // modify header
    function isLogin(){
        if(localStorage.hasOwnProperty("UserName")){
            document.getElementById("login").style.display = 'none';
            document.getElementById("login-out").style.display = 'block';
            document.getElementById("profile").style.display = 'block';
            document.querySelector("#profile a").innerHTML = 'User: ' + localStorage.getItem('UserName');
        }
        else {
            document.getElementById("login-out").style.display = 'none';
            document.getElementById("login").style.display = 'block';
            document.getElementById("profile").style.display = 'none';
        }
    }
    isLogin();
    isDBExist();

}